package com.open.center.jpa.model.page;

import java.util.LinkedList;
import java.util.List;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import org.springframework.data.domain.Sort;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import com.open.center.jpa.model.page.by.DateTimeBy;
import com.open.center.jpa.model.page.by.FilterBy;
import com.open.center.jpa.model.page.by.LongRangeBy;
import com.open.center.jpa.model.page.by.SearchBy;
import com.open.center.jpa.model.page.by.SortBy;

/**
 * 查询基类
 *
 * @author Riche's
 * @since 2025/5/23
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Query {

    @Schema(name = "sortBys", description = "排序")
    private LinkedList<SortBy> sortBys;

    @Schema(name = "searchBys", description = "模糊匹配查询")
    private List<SearchBy<?>> searchBys;

    @Schema(name = "filterBys", description = "精确匹配查询")
    private List<FilterBy<?>> filterBys;

    @Schema(name = "dateTimeBys", description = "日期时间查询")
    private List<DateTimeBy> dateTimeBys;

    @Schema(name = "longRangeBys", description = "Long类型区间查询")
    private List<LongRangeBy> longRangeBys;

    /**
     * 获取排序信息
     */
    public Sort sort() {
        if (CollectionUtils.isEmpty(sortBys)) {
            return Sort.by(Sort.Direction.DESC, "createTime");
        }
        return Sort.by(sortBys.stream()
                .filter(s -> !ObjectUtils.isEmpty(s.getKey()))
                .map(s -> new Sort.Order(s.getDirection(), s.getKey()))
                .toList()
        );
    }
}
